package com.diandiansong.app.ui.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.diandiansong.app.R;
import com.diandiansong.app.entity.AddressInfo;
import com.diandiansong.app.entity.CityInfo;
import com.diandiansong.app.entity.UploadInfo;
import com.diandiansong.app.ui.base.BaseAct;
import com.diandiansong.app.utils.A;
import com.diandiansong.app.utils.AddressPickTask;
import com.diandiansong.app.utils.Api;
import com.yanzhenjie.album.Album;

import java.io.File;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import cn.qqtheme.framework.entity.City;
import cn.qqtheme.framework.entity.County;
import cn.qqtheme.framework.entity.Province;
import diandiansong.library.utils.BaseEntity;
import diandiansong.library.utils.CCallBack;
import diandiansong.library.utils.ImgLoader;

/**
 * 注册（第二步）
 */
public class RegisterStep2 extends BaseAct {

    static String mPhone;
    static String mCode;
    static String mPwd;
    @BindView(R.id.et_shop_name)
    EditText mEtShopName;
    @BindView(R.id.et_area)
    TextView mEtArea;
    @BindView(R.id.et_address)
    EditText mEtAddress;
    @BindView(R.id.et_shoper)
    EditText mEtShoper;
    @BindView(R.id.et_invite_code)
    EditText mEtInviteCode;
    @BindView(R.id.iv_business_licence)
    ImageView mIvBusinessLicence;
    @BindView(R.id.do_next)
    TextView mDoNext;

    AddressInfo.Data mAddress;

    public static void start(Context context, String phone, String code, String pwd) {
        mPhone = phone;
        mCode = code;
        mPwd = pwd;
        Intent starter = new Intent(context, RegisterStep2.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addContentView(R.layout.user_register_step2);

        initTitle("注册账户");
    }


    @OnClick(R.id.do_next)
    public void onDoNextClicked() {

        String company = getText(mEtShopName);
        String address = getText(mEtAddress);
        String realname = getText(mEtShoper);
        String iCode = getText(mEtInviteCode);
        if (company.isEmpty()) {
            toast("请填写公司名");
            return;
        }

        if (realname.isEmpty()) {
            toast("请填写姓名");
            return;
        }

        if (address.isEmpty()) {
            toast("请填写详细地址");
            return;
        }

        if (mAddress == null) {
            toast("请选择地区");
            return;
        }

        /**
         * mobile,
         verif_code,
         password_one,
         company,
         realname,
         province,
         citys,
         county,
         address,
         inviter_code,
         license_img,
         */
        A.register(mPhone, mCode, mPwd, company, realname, mAddress.getProvinceId(), mAddress.getCitysId(), mAddress.getCountyId(), address, iCode,
                   mLisenUrl,
                   RegisterStep1.openId, this, new CCallBack<BaseEntity>() {
                    @Override
                    public void onSuccess(BaseEntity baseEntity) throws Exception {
                        Login.start(getActivity());
                        RegisterStep1.doFinish();
                        finish();
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        toast(msg);
                    }
                }
        );
    }

    @OnClick(R.id.et_area)
    public void onMEtAreaClicked() {
        A.city(this, new CCallBack<CityInfo>() {
            @Override
            public void onSuccess(CityInfo cityInfo) throws Exception {
                CityInfo.saveCityInfo(getActivity(), cityInfo);
                AddressPickTask task = new AddressPickTask(getActivity());
                task.setHideProvince(false);
                task.setHideCounty(false);
                task.setCallback(new AddressPickTask.Callback() {
                    @Override
                    public void onAddressInitFailed() {
                        toast("数据初始化失败");
                    }

                    @Override
                    public void onAddressPicked(Province province, City city, County county) {
                        mAddress = new AddressInfo.Data();
                        if (county == null) {
                            //toast(province.getAreaName() + city.getAreaName());
                            mAddress.setProvince(province.getAreaName());
                            mAddress.setProvinceId(province.getAreaId());
                            mAddress.setCitys(city.getAreaName());
                            mAddress.setCitysId(city.getAreaId());
                            mAddress.setCounty("");
                            mAddress.setCountyId("");

                            setText(mEtArea, province.getAreaName() + city.getAreaName());

                        } else {
                            //toast(province.getAreaName() + city.getAreaName() + county.getAreaName());
                            mAddress.setProvince(province.getAreaName());
                            mAddress.setProvinceId(province.getAreaId());
                            mAddress.setCitys(city.getAreaName());
                            mAddress.setCitysId(city.getAreaId());
                            mAddress.setCounty(county.getAreaName());
                            mAddress.setCountyId(county.getAreaId());

                            setText(mEtArea, province.getAreaName() + city.getAreaName() + county.getAreaName());
                        }
                    }
                });
                task.execute("", "", "");
            }

            @Override
            public void onFailure(int code, String msg) {
                toast(msg);
            }
        });
    }

    @OnClick(R.id.iv_business_licence)
    public void onMIvBusinessLicenceClicked() {
        Album.album(this)
                .requestCode(999) // Request code.
                //.toolBarColor(toolbarColor) // Toolbar color.
                //.statusBarColor(statusBarColor) // StatusBar color.
                //.navigationBarColor(navigationBarColor) // NavigationBar color.
                .title("选择图片") // Title.
                .selectCount(1) // Choose up to a few pictures.
                .columnCount(2) // Number of albums.
                .camera(true) // Have a camera function.
                //.checkedList(mImageList) // Has selected the picture, automatically select.
                .start();
    }

    String mLisenUrl;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 999) {
            if (resultCode == RESULT_OK) { // Successfully.
                // Parse select result.
                final ArrayList<String> imageList = Album.parseResult(data);
                if (imageList.size() > 0) {
                    onLoading();
                    Api.upload(new File(imageList.get(0)), new CCallBack<UploadInfo>() {
                        @Override
                        public void onSuccess(UploadInfo uploadInfo) throws Exception {
                            onLoaded();
                            mLisenUrl = uploadInfo.getData().getUrl();
                            mLisenUrl = A.URL + mLisenUrl;///uploads/2018-12-19/d599540407dda58e17d44e2a52b9875f60.jpg
                            //http://yidiann.com/uploads/2018-12-19/d599540407dda58e17d44e2a52b9875f60.jpg
                            ImgLoader.display(mIvBusinessLicence, "file://" + imageList.get(0));
                        }

                        @Override
                        public void onFailure(int code, String msg) {
                            onLoaded();
                            toast(msg);
                        }
                    });
                }

            } else if (resultCode == RESULT_CANCELED) {
                // User canceled.
            }
        }
    }

}